//
//  CYAlert.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/8/14.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYAlert.h"

@interface CYAlert ()

@end

@implementation CYAlert
+ (void)showWithTitle:(NSString *)title message:(NSString *)message inController:(CYViewController *)controller complete:(void(^)(void))completion{
    
    WS(controller)
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"确定"
                                                       style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         if (completion)
                                                             completion();
                                                     }];
    [alertVC addAction:enterBtn];
    [weakSelf presentViewController:alertVC animated:YES completion:nil];
}

+ (void)showInWindowWithTitle:(NSString *)title message:(NSString *)message complete:(void(^)(void))completion{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         if (completion)
                                                             completion();
                                                     }];
    [alertVC addAction:enterBtn];
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window makeKeyAndVisible];
    [window.rootViewController presentViewController:alertVC animated:YES completion:nil];
    
}

+ (void)showInWindowWithTitle:(NSString *)title message:(NSString *)message enterComplete:(void(^)(void))completion{

    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:title
                                                                     message:message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *enterBtn = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault
                                                     handler:^(UIAlertAction * _Nonnull action) {
                                                         if (completion)
                                                             completion();
                                                     }];
    UIAlertAction *cancelBtn = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel
                                                     handler:nil];
    [alertVC addAction:enterBtn];
    [alertVC addAction:cancelBtn];

    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window makeKeyAndVisible];
    [window.rootViewController presentViewController:alertVC animated:YES completion:nil];




}
+ (void)showMessage:(NSString *)message{
    
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil
                                                                     message:message
                                                              preferredStyle:UIAlertControllerStyleAlert];
    //修改message
    NSMutableAttributedString *alertVCMessage = [[NSMutableAttributedString alloc] initWithString:message];
    //    [alertVCMessage addAttribute:NSForegroundColorAttributeName value:[UIColor greenColor] range:NSMakeRange(0, 4)];
    [alertVCMessage addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:AL(15) weight:UIFontWeightLight] range:NSMakeRange(0, message.length)];
    [alertVC setValue:alertVCMessage forKey:@"attributedMessage"];
    
    UIWindow *window = [UIApplication sharedApplication].delegate.window;
    [window makeKeyAndVisible];
    
    [window.rootViewController presentViewController:alertVC animated:YES completion:^{
        dispatch_time_t delaytime = dispatch_time(DISPATCH_TIME_NOW, (int64_t) 2.0 *NSEC_PER_SEC);//2秒
        dispatch_after(delaytime, dispatch_get_main_queue(), ^{
            [alertVC dismissViewControllerAnimated:YES completion:^{//2秒后，返回

            }];
        });
    }];
    
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
